package com.ksyun.seckill.controller.offline;


import com.ksyun.seckill.dto.CampaignItemDto;
import com.ksyun.seckill.rest.RestResult;
import com.ksyun.seckill.service.CampaignService;
import com.ksyun.seckill.vo.CreateCampaignVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;


/**
 * 管理员端controller
 */
@RestController
@RequestMapping(value = "/offline/campaign", produces = {MediaType.APPLICATION_JSON_VALUE})
@Slf4j
public class OfflineCampaignController {

    @Autowired
    CampaignService campaignService;

    /**
     * get请求，获取活动列表
     * @return
     *
     */
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public RestResult list() {
        List<CampaignItemDto> list=campaignService.selectCampaignList();
        return RestResult.success().data(list);
    }


    /**
     * post请求，创建活动
     * @param createCampaignVO
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public RestResult save(@Valid @RequestBody CreateCampaignVO createCampaignVO){
        boolean isSuccess = campaignService.saveCampaign(createCampaignVO);
        return isSuccess?RestResult.success().msg("ok"):RestResult.failure().msg("创建失败!");
    }


    /**
     * post请求，根据活动id进行相关活动的状态更改，上线活动。
     * @param campaignId
     * @return
     */
    @RequestMapping(value = "/online",method = RequestMethod.POST)
    public RestResult online(@RequestParam(value = "campaignId") Long campaignId){
        return campaignService.onlineCampaign(campaignId);
    }

    /**
     * post请求，根据活动id进行相关活动的状态更改，下线活动。
     * @param campaignId
     * @return
     */
    @RequestMapping(value = "/offline",method = RequestMethod.POST)
    public RestResult offline(@RequestParam(value = "campaignId") Long campaignId){
        return campaignService.offlineCampaign(campaignId);
    }

    /**
     * post请求，根据活动id进行删除对应的活动。
     * @param campaignId
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public RestResult delete(@RequestParam(value = "campaignId") Long campaignId){

        return campaignService.deleteCampaign(campaignId);
    }



}
